﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using System.Data;
using CrystalDecisions.Shared;

namespace CR_With_MVC.AspNetForms
{
    public partial class aspnetsimple : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            bool isValid = true;

            //ReportDocument rd = new ReportDocument();
            //rd.Load(Server.MapPath("~/Rpts/") + "rptSumMeterByDate.rpt");
            //CrystalReportViewer1.ReportSource = rd;


            string strReportName = System.Web.HttpContext.Current.Session["ReportName"].ToString();    // Setting ReportName
            string strProduct = System.Web.HttpContext.Current.Session["rptProduct"].ToString();     // Setting FromDate 
            string strMonthShow = System.Web.HttpContext.Current.Session["rptMonthShow"].ToString();
            string strFromDate = System.Web.HttpContext.Current.Session["rptFromDate"].ToString();     // Setting FromDate 
            string strToDate = System.Web.HttpContext.Current.Session["rptToDate"].ToString();         // Setting ToDate    
            DataTable _dt = (DataTable)System.Web.HttpContext.Current.Session["rptSource"];

            if (isValid)
            {
                _dt.TableName = "dt_SumMeterByDate";
                DataSet ds = new DataSet();
                ds.Merge(_dt);
                using (ReportClass rptH = new ReportClass())
                {
                    ReportDocument rd = new ReportDocument();
                    string strRptPath = System.Web.HttpContext.Current.Server.MapPath("~/") + "Rpts//" + strReportName;

                    rd.Load(strRptPath);
                    if (_dt != null && _dt.Rows.Count > 0)
                    {
                        rd.SetDataSource(ds);

                        if (!string.IsNullOrEmpty(strProduct))
                            rd.SetParameterValue("product", strProduct);
                        if (!string.IsNullOrEmpty(strMonthShow))
                            rd.SetParameterValue("monthshow", strMonthShow);
                        if (!string.IsNullOrEmpty(strFromDate))
                            rd.SetParameterValue("fromDate", strFromDate);
                        if (!string.IsNullOrEmpty(strToDate))
                            rd.SetParameterValue("toDate", strToDate);

                        //rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
                        CrystalReportViewer1.ReportSource = rd;

                        ConnectionInfo myConnectionInfo = new ConnectionInfo();
                        myConnectionInfo.ServerName = "61.19.248.21";
                        myConnectionInfo.DatabaseName = "DVS";
                        myConnectionInfo.UserID = "dvsuser";
                        myConnectionInfo.Password = "adminsql";
                        setDBLOGONforREPORT(myConnectionInfo);
                    }

                  // rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
                }


                // Clear all sessions value
                //Session["ReportName"] = null;
                //Session["rptProduct"] = null;
                //Session["rptMonthShow"] = null;
                //Session["rptFromDate"] = null;
                //Session["rptToDate"] = null;
                //Session["rptSource"] = null;
            }
            else
            {
                Response.Write("<H2>Nothing Found; No Report name found</H2>");
            }
        }

        private void setDBLOGONforREPORT(ConnectionInfo myconnectioninfo)
        {
            TableLogOnInfos mytableloginfos = new TableLogOnInfos();
            mytableloginfos = CrystalReportViewer1.LogOnInfo;
            foreach (TableLogOnInfo myTableLogOnInfo in mytableloginfos)
            {
                myTableLogOnInfo.ConnectionInfo = myconnectioninfo;
            }

        }
    }
}